﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Kiva7.Utilities
{
    public class Scraper
    {
        private readonly ScraperClient _client;

        public Scraper()
        {
            _client = new ScraperClient();
        }

        private ScraperClient Client
        {
            get { return _client; }
        }

        public void Scrape()
        {
            Client.HttpGet(new Uri("https://www.kiva.org/login", UriKind.Absolute), LoginHandler,  null);
        }

        private string FindCrumbValue(string result)
        {
            int i = result.IndexOf(@"name=""crumb""", StringComparison.InvariantCulture);

            if (i < 0)
                return null;

            i = result.IndexOf(@"value=", i, StringComparison.InvariantCulture);
            if (i < 0)
                return null;

            i = result.IndexOf(@"""", i+1, StringComparison.InvariantCulture);
            if (i < 0)
                return null;
            int j = result.IndexOf(@"""", i+1, StringComparison.InvariantCulture);
            if (j < 0)
                return null;
            string a = result.Substring(i + 1, (j - i) - 1);
            return a;
        }

        private void LoginHandler(string result, Uri responseUri, object userState)
        {
            PostDataBuilder data =new PostDataBuilder();

            data.Add("email", "jonkeda@hotmail.com");
            data.Add("password", "boppie12aa");
            data.Add("crumb", FindCrumbValue(result));
            data.Add("currURL", "https://www.kiva.org/login");

            // https://www.kiva.org/login/process?doneUrl=https%3A%2F%2Fwww.kiva.org%2Fportfolio
            Client.HttpPost(new Uri("https://www.kiva.org/login/process", UriKind.Absolute), AccountHandler, data, null);
        }

        private void AccountHandler(string result, Uri responseUri, object userState)
        {
            // Request timed out. Please try again. If you are still having problems, refresh the page.
            bool timedout = result.Contains("Request timed out.");

            if (!timedout)
            //{
            //    PostDataBuilder data = new PostDataBuilder();

            //    data.Add("email", "jonkeda@hotmail.com");
            //    data.Add("password", "boppie12");

            //    // https://www.kiva.org/login/process?doneUrl=https%3A%2F%2Fwww.kiva.org%2Fportfolio\
            //    Client.HttpPost(new Uri("https://www.kiva.org/login/process?doneUrl=https%3A%2F%2Fwww.kiva.org%2Fportfolio", UriKind.Absolute), AccountHandler, data, null);
            //}
            //else
            //{
                Client.HttpGet(new Uri("https://www.kiva.org/portfolio", UriKind.Absolute), PortfolioHandler, null);    
            //}
        }

        private void PortfolioHandler(string result, Uri responseUri, object userState)
        {}
    }
}
